package com.bsb.biz.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsb.biz.service.BsbFirmProductService;
import com.bsb.biz.shiro.UserUtils;
import com.bsb.base.common.config.Global;
import com.bsb.base.common.util.DateUtils;
import com.bsb.base.common.util.IdGen;
import com.bsb.base.model.BsbFirmProduct;
import com.bsb.base.model.BsbFirmProductExample;
import com.bsb.base.model.BsbFirmProductExample.Criteria;
import com.bsb.base.model.PageRes;
import com.bsb.base.model.SysUser;

@Controller
public class BsbFirmProductController{
	
	private int defaultPageSize=10;
	
	//private String redirect = "redirect:/bsbFirmProduct/list.do";
	
	@Resource
	private BsbFirmProductService bsbFirmProductService;
	
	
	@RequiresPermissions("bsbFirmProduct:view")
	@RequestMapping(value="/bsbFirmProduct/list",method=RequestMethod.GET)
	public String list(
			Model model,@RequestParam Map<String, Object> params,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		BsbFirmProductExample bsbFirmProductExample = new BsbFirmProductExample();
		int totalCount = bsbFirmProductService.countBsbFirmProduct(bsbFirmProductExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
	
		
		
		List<BsbFirmProduct> bsbFirmProducts = bsbFirmProductService.getPageBsbFirmProduct(bsbFirmProductExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbFirmProducts", bsbFirmProducts);
		return "/BsbFirmProduct/list";
	}
	
	@RequestMapping(value="/firmProduct/list",method=RequestMethod.GET)
	@ResponseBody
	public Object listProduct(@RequestParam Map<String, Object> params,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		PageRes<List<BsbFirmProduct>> res =new PageRes<List<BsbFirmProduct>>();
		try {
			int maxPageNum = 0;
			if (pageSize == null) {
				pageSize = defaultPageSize;
			}
			SysUser currentUser = UserUtils.currentUser();
			BsbFirmProductExample bsbFirmProductExample = new BsbFirmProductExample();
			Long firmId = currentUser.getFirmId();
			if (firmId==null) {
				res.setStatus(PageRes.FAIL_CODE);
				res.setMsg("账号异常请联系管理员！");
				return res;
			}
			String name = (String) params.get("name");
			String delflag = (String) params.get("delflag");
			String start = (String) params.get("start");
			Date startDate = DateUtils.parseDate(start);
			String end = (String) params.get("end");
			Date endDate = DateUtils.parseDate(end);
			
			Criteria createCriteria = bsbFirmProductExample.createCriteria();
			createCriteria.andFirmIdEqualTo(firmId.intValue());
			if(StringUtils.isNotBlank(name)){
				createCriteria.andTitleLike("%"+name+"%");
			}
			if(startDate!=null){
				createCriteria.andCreateDateGreaterThanOrEqualTo(startDate);
			}	
			
			if(endDate!=null){
				endDate=DateUtils.addDays(endDate, 1);
				createCriteria.andCreateDateLessThanOrEqualTo(endDate);
			}
			if(StringUtils.isNotBlank(delflag)){
				createCriteria.andDelFlagEqualTo(delflag);
			}
			
			
			int totalCount = bsbFirmProductService.countBsbFirmProduct(bsbFirmProductExample);
			if(totalCount%pageSize == 0){
				maxPageNum = totalCount/pageSize;
			}else{
				maxPageNum = totalCount/pageSize+1;
			}
			if (pageNum == null || pageNum < 1) {
				pageNum = 1;
			}else {
				if(maxPageNum > 0 && pageNum > maxPageNum) {
					pageNum = maxPageNum;
				}
			}
			bsbFirmProductExample.setOrderByClause("bsb_firm_product.createDate desc");
			bsbFirmProductExample.setPageIndex((pageNum - 1) * pageSize);
			bsbFirmProductExample.setPageSize(pageSize);
			List<BsbFirmProduct> bsbFirmProducts = bsbFirmProductService.getPageBsbFirmProduct(bsbFirmProductExample);
			for (BsbFirmProduct bsbFirmProduct : bsbFirmProducts) {
				bsbFirmProduct.setContent(null);
			}
			res.setStatus(PageRes.SUCCESS_CODE);
			res.setMaxPageNum(maxPageNum);
			res.setPageNum(pageNum);
			res.setPageSize(pageSize);
			res.setTotalCount(totalCount);
			res.setData(bsbFirmProducts);
		} catch (Exception e) {
			e.printStackTrace();
			res.setStatus(PageRes.FAIL_CODE);
			res.setMsg(PageRes.FAIL_DES);
		}
		return res;
	}
	
	@RequestMapping("/bsbFirmProduct/toAdd")
	public String toAdd(Model model,HttpServletRequest request) {
		SysUser currentUser = UserUtils.currentUser();
		Long firmId = currentUser.getFirmId();
		if(firmId==null){
			return "redirect:/bsbFirmProduct/toList.do";
		}
		 
		String queryString = request.getQueryString();
		model.addAttribute("queryString", queryString);
		
		return "/BsbFirmProduct/add";
	}
	@RequiresPermissions("bsbFirmProduct:add")
	@RequestMapping("/bsbFirmProduct/add")
	public String add(Model model,@ModelAttribute("bsbFirmProduct") BsbFirmProduct bsbFirmProduct) {
		SysUser currentUser = UserUtils.currentUser();
		String content = bsbFirmProduct.getContent();
		content = StringEscapeUtils.unescapeHtml(content);
		bsbFirmProduct.setContent(content);
		bsbFirmProduct.setCreateDate(new Date());
		bsbFirmProduct.setCreateBy(currentUser.getUsername());
		bsbFirmProduct.setDelFlag(Global.SHOW);
		Long firmId = currentUser.getFirmId();
		if(firmId!=null){
			bsbFirmProduct.setFirmId(currentUser.getFirmId().intValue());
			bsbFirmProduct.setUuid(IdGen.uuid());
			bsbFirmProductService.addBsbFirmProduct(bsbFirmProduct);
		}
		
		
		
		return "redirect:/bsbFirmProduct/toList.do";
	}
	@RequiresPermissions("bsbFirmProduct:del")
	@RequestMapping("/bsbFirmProduct/del")
	@ResponseBody
	public Object del(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		Map<String, String> res=new HashMap<String, String>();
		try {
			bsbFirmProductService.delBsbFirmProductByUuid(uuid);
			res.put("status", "1");
			res.put("msg", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			res.put("status", "0");
			res.put("msg","网络异常");
		}
		return res;
	}
	@RequiresPermissions("bsbFirmProduct:delflag")
	@RequestMapping("/bsbFirmProduct/delflag")
	@ResponseBody
	public Object delFlag(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		
		Map<String, String> res=new HashMap<String, String>();
		try {
			BsbFirmProduct bsbFirmProduct = bsbFirmProductService.getBsbFirmProductByUuid(uuid);
			if(bsbFirmProduct!=null){
				BsbFirmProduct bsbFirmProduct2 = new BsbFirmProduct();
				bsbFirmProduct2.setUuid(uuid);
				
				String delFlag = bsbFirmProduct.getDelFlag();
				if(delFlag.equals(Global.SHOW)){
					bsbFirmProduct2.setDelFlag(Global.HIDE);
					res.put("msg", "隐藏成功");
				}else {
					bsbFirmProduct2.setDelFlag(Global.SHOW);
					res.put("msg", "显示成功");
				}
				bsbFirmProductService.updateBsbFirmProduct(bsbFirmProduct2);
				res.put("status", "1");
				
			}else {
				res.put("status", "3");
				res.put("msg", "未查到此数据！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			res.put("status", "0");
			res.put("msg","异常");
		}
	
		return res;
	}

	@RequestMapping("/bsbFirmProduct/toEdit")
	public String toEdit(Model model,HttpServletRequest request,
			@RequestParam(value = "uuid", required = false) String uuid) {
		
		BsbFirmProduct bsbFirmProduct = bsbFirmProductService.getBsbFirmProductByUuid(uuid);
		model.addAttribute("bsbFirmProduct", bsbFirmProduct);
		String queryString = request.getQueryString();
		model.addAttribute("queryString", queryString);
		return "/BsbFirmProduct/edit";
	}

	
	
	
	
	@RequiresPermissions("bsbFirmProduct:edit")
	@RequestMapping("/bsbFirmProduct/edit")
	public String edit(Model model, 
			@RequestParam(value = "queryString", required = false) String queryString,
			@ModelAttribute("bsbFirmProduct") BsbFirmProduct bsbFirmProduct) {
		SysUser currentUser = UserUtils.currentUser();
		String username = currentUser.getUsername();
		Date date = new Date();
		bsbFirmProduct.setUpdateBy(username);
		bsbFirmProduct.setUpdateDate(date);
		String content = bsbFirmProduct.getContent();
		content = StringEscapeUtils.unescapeHtml(content);
		bsbFirmProduct.setContent(content);
		bsbFirmProductService.updateBsbFirmProduct(bsbFirmProduct);
		
		
		model.addAttribute("queryString", queryString);
		
		return "redirect:/bsbFirmProduct/toList.do"+ "?" +queryString;
	}
	@RequiresPermissions("bsbFirmProduct:toList")
	@RequestMapping(value="/bsbFirmProduct/toList",method=RequestMethod.GET)
	public String toList(Model model,@RequestParam Map<String, Object> params,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		
		model.addAttribute("pages", pageNum);
		String name = (String) params.get("name");
		String delflag = (String) params.get("delflag");
		String start = (String) params.get("start");
		String end = (String) params.get("end");
		model.addAttribute("name", name);
		model.addAttribute("delflag", delflag);
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		
		return"/BsbFirmProduct/list";
	}

	@RequestMapping(value="/bsbFirmProduct/{uuid}/view",method = RequestMethod.GET)
	public String view(Model model,@PathVariable("uuid") String uuid) {
		BsbFirmProduct bsbFirmProduct = bsbFirmProductService.getBsbFirmProductByUuid(uuid);
		model.addAttribute("bsbFirmProduct", bsbFirmProduct);
		return "/BsbFirmProduct/view";
	}
	@RequiresPermissions("bsbFirmProduct:view")
	@RequestMapping(value="/bsbFirmProduct/view",method = RequestMethod.GET)
	public String view(Model model,HttpServletRequest request,
			@RequestParam(value = "uuid", required = false) String uuid) {
		
		BsbFirmProduct bsbFirmProduct = bsbFirmProductService.getBsbFirmProductByUuid(uuid);
		model.addAttribute("bsbFirmProduct", bsbFirmProduct);
		String queryString = request.getQueryString();
		model.addAttribute("queryString", queryString);
		return "/BsbFirmProduct/view";
	}

	
	
	@RequestMapping(value="/bsbFirmProduct/back",method = RequestMethod.GET)
	public String back(Model model, 
			@RequestParam(value = "queryString", required = false) String queryString) {
		return "redirect:/bsbFirmProduct/toList.do"+ "?" +queryString;
	}
}
